package com.wuzihao.calculator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator extends JFrame implements ActionListener {
    //北部组件
    private JPanel jPanel_north = new JPanel();
    private JTextField textField = new JTextField();
    private JButton jButton = new JButton();
    //中部组件
    private JPanel jPanel_Center = new JPanel();

    public void init(){
        this.setTitle(Constant.NAME);
        this.setVisible(true);
        this.setSize(Constant.FRAME_W, Constant.FRAME_H);
        this.setLocation(Constant.FRAME_X, Constant.FRAME_Y);
        this.setLayout(new BorderLayout());
        this.setResizable(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void Component_North(){
        this.textField.setPreferredSize(new Dimension(600,50));
        this.jButton.setText("清空");
        this.jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textField.setText("");
            }
        });
        jPanel_north.add(textField);
        jPanel_north.add(jButton);
        this.add(jPanel_north,BorderLayout.NORTH);
    }

    public void Component_Center(){
        String str = "123+456-789*0.=/";
        String regex = "[\\+\\-*=/]";
        this.jPanel_Center.setLayout(new GridLayout(4,4));
        for (int i = 0; i < 16; i++) {
            String substring = str.substring(i, i + 1);
            JButton jButton = new JButton();
            jButton.setText(substring);
            if(substring.matches(regex)){
                jButton.setFont(new Font("粗体",Font.BOLD,16));
            }
            jButton.addActionListener(this);
            jPanel_Center.add(jButton);
        }
        this.add(jPanel_Center,BorderLayout.CENTER);
    }

    public Calculator(){
        this.init();
        Component_North();
        Component_Center();
    }

    private String firstInput = null;
    private String operator = null;

    @Override
    public void actionPerformed(ActionEvent e) {
        String click = e.getActionCommand();
        if(".0123456789".contains(click)){
            this.textField.setText(textField.getText() + click);
        }else if(click.matches("[\\+\\-*/]{1}")){
            operator = click;
            firstInput = this.textField.getText();//保存当前的值
            this.textField.setText("");
        }else if(click.equals("=")){
            Double a = Double.valueOf(firstInput);
            Double b = Double.valueOf(textField.getText());
            Double result = null;

            switch(operator){
                case "+":
                    result = a + b;
                    break;
                case "-":
                    result = a - b;
                    break;
                case "*":
                    result = a * b;
                    break;
                case "/":
                    result = a / b;
                    break;
            }

            this.textField.setText(result.toString());
        }
    }
}
